using System.IO;
using System.Text;
using NUnit.Framework;

namespace Xtee.Core.Test
{
    [TestFixture]
    public class XmlSanitizingStreamTest
    {

        [Test]
        public void ReadOnlyReturnsLegalXmlCharacters()
        {
            // This should be stripped to "\t\r\n<>:"
            StringBuilder sb = new StringBuilder();
            sb.Append((char)0x0E); //niceButForbittenHexChar
            sb.Append("\0\t\a\r\b\n<>:");
            sb.Append((char)0x17); //uglyForbittenHexChar

            string resulXml = sb.ToString();

            // Load the XML as a Stream
            using (var buffer = new MemoryStream(System.Text.Encoding.Default.GetBytes(resulXml)))
            {
                using (var sanitizer = new XmlSanitizingStream(buffer))
                {
                    Assert.AreEqual(sanitizer.Read(), '\t');
                    Assert.AreEqual(sanitizer.Read(), '\r');
                    Assert.AreEqual(sanitizer.Read(), '\n');
                    Assert.AreEqual(sanitizer.Read(), '<');
                    Assert.AreEqual(sanitizer.Read(), '>');
                    Assert.AreEqual(sanitizer.Read(), ':');
                    Assert.AreEqual(sanitizer.Read(), -1);
                    Assert.IsTrue(sanitizer.EndOfStream);
                }
            }

            using (var buffer = new MemoryStream(System.Text.Encoding.Default.GetBytes(resulXml)))
            {
                using (var sanitizer = new XmlSanitizingStream(buffer))
                {
                    Assert.AreEqual(sanitizer.ReadToEnd(), "\t\r\n<>:");
                }
            }

        } // method

    }
}